package com.xhdx.modules.jbpm4.tool;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * <p>className: JbpmExecutionTool</p>
 * <p>description: 流程执行服务接口工具类</p>
 *
 * @author xhdx
 * @version 1.0.0
 * @date 2020-05-24 21:33
 */
@Slf4j
@Data
@Component("jbpmExecutionTool")
public class JbpmExecutionTool {

    /**
     * ExecutionService-流程执行服务接口
     * <br> 提供启动流程实例、"执行"推进、设置流程变量等操作。
     */
    @Autowired
    @Qualifier(value = "executionService")
    private ExecutionService executionService;


    /***
     * 创建流程实例
     *
     * @author
     * @date 2020/5/24 22:40
     * @param processDefinitionKey 流程定义键
     * @return org.jbpm.api.ProcessInstance
     */
    public ProcessInstance startProcessInstanceByKey(String processDefinitionKey) {
        log.info("参数，processDefinitionKey: {} ", processDefinitionKey);
        ProcessInstance processInstance = executionService.startProcessInstanceByKey(processDefinitionKey);
        log.info("流程实例ID:{}", processInstance.getId());
        return processInstance;
    }

    /***
     * 发起流程实例并传入流程变量
     *
     * @author
     * @date 2020/5/23 20:01
     * @param processDefinitionKey  流程定义键
     * @param variables 流程变量
     * @return org.jbpm.api.ProcessInstance
     */
    public ProcessInstance startProcessInstanceByKey(String processDefinitionKey, Map<String, Object> variables) {
        log.info("参数，processDefinitionKey: {}  variables:{}", processDefinitionKey, variables);
        ProcessInstance processInstance = executionService.startProcessInstanceByKey(processDefinitionKey, variables);
        return processInstance;
    }


    /***
     * 创建流程实例（指定业务键）
     *
     * <br> 生成的流程实例ID格式：{processDefinitionKey}.{bizKey}
     *
     * @author
     * @date 2020/5/24 22:40
     * @param processDefinitionKey 流程定义键
     * @param bizKey  业务键（比如：订单号/流水号/保险单号）
     * @return org.jbpm.api.ProcessInstance
     */
    public ProcessInstance startProcessInstanceByBizKey(String processDefinitionKey, String bizKey) {
        log.info("参数，processDefinitionKey: {}  processInstanceKey:{}", processDefinitionKey, bizKey);
        ProcessInstance processInstance = executionService.startProcessInstanceByKey(processDefinitionKey, bizKey);
        log.info("流程实例ID:{}", processInstance.getId());
        return processInstance;
    }


    /***
     * 发出触发执行外部信号
     *
     * @author
     * @date 2020/5/28 21:08
     * @param executionId 执行ID
     * @return org.jbpm.api.ProcessInstance
     */
    public ProcessInstance signalExecutionById(String executionId) {
        //调用ExecutionService的signalExecutionById的方法会触发一个外部信号，触发流程走向下一步
        log.info("参数，executionId: {}", executionId);
        return executionService.signalExecutionById(executionId);
    }

    /***
     * 发出触发执行外部信号(指定信号名称)
     *
     * @author
     * @date 2020/5/28 21:08
     * @param executionId 执行ID
     * @param signalName 信号名称
     * @return org.jbpm.api.ProcessInstance
     */
    public ProcessInstance signalExecutionById(String executionId, String signalName) {
        //调用ExecutionService的signalExecutionById的方法会触发一个外部信号，触发流程走向下一步
        log.info("参数，executionId: {}，signalName:{}", executionId, signalName);
        return executionService.signalExecutionById(executionId, signalName);
    }


}